
// 此文件放置低代码公共函数

import { ECBillApproveStatus, ECBillStatus, lowcodeRowKey } from "./constant";
import type { LowcodeFiledDB, LowcodeFormGridSortProxy, LowcodePagePanelComponentItem } from "./interface";

/**
 * @description 与 @enjoy/mobile 组件对应
 * c_type === '1' ---> 尚未审批; c_type === '2' ---> 正在审批
 * c_type === '3' ---> 已审核; c_type === '4' ---> 未通过
 * 
 * @req 汉字类型的单据状态 如： 尚未审批
 */
// export const status2Type = (status: string) => {
//   const enumConf: { [key: string]: string } = { "尚未审批": "1", "正在审批": "2", "已审核": "3", "未通过": "4" };
//   return enumConf[status] || status || "";
// };

export const isLowcodeNumComponent = (proxy: { c_type?: string; c_valuetype?: string; bindFieldDBType?: LowcodeFiledDB }) => {
	const { c_type, c_valuetype, bindFieldDBType } = proxy || {};
	const isNumVal = ["2", "3"].includes(c_valuetype || "") || c_type === "numberinput" || ["2", "3"].includes(bindFieldDBType || "");
	return isNumVal;
};

/**
 * @description 对栅格字段进行行和列的排序
 * @req 
 */
export const formGridSort = (Components?: LowcodePagePanelComponentItem[], proxy?: LowcodeFormGridSortProxy) => {
	if (!Array.isArray(Components) || !Components?.length) {
		return {};
	}

  const { excludeFields } = proxy || {};

	const cacheComponents: { [key: string]: LowcodePagePanelComponentItem[] } = {};
	const cacheComponentsConf: { [key: string]: LowcodePagePanelComponentItem } = {};

	const cacheComponentsHead: { [key: string]: LowcodePagePanelComponentItem[] } = {};
	const cacheComponentsHeadConf: { [key: string]: LowcodePagePanelComponentItem } = {};

	Array.from(Components)?.forEach(c => {
		if (!c?.c_code) {
			return;
		}
		const cRowIdx = c?.c_row_index;
		if (Array.isArray(excludeFields) && excludeFields.length && excludeFields.includes(c.c_code)) {
			return;
		}

		if (c?.c_isheader === "1") {
			cacheComponentsHeadConf[c?.c_code || ""] = c;
			if (c?.c_hide + "" === "0" && (cRowIdx || cRowIdx === 0)) {
				cacheComponentsHead[cRowIdx] = (cacheComponentsHead[cRowIdx] || []).concat(c);
			}
			return;
		}

		cacheComponentsConf[c?.c_code || ""] = c;
		if (c?.c_hide + "" === "0" && (cRowIdx || cRowIdx === 0)) {
			cacheComponents[cRowIdx] = (cacheComponents[cRowIdx] || []).concat(c);
		}
	});

	Object.values(cacheComponents)?.forEach(rowCols => rowCols?.sort((ca, cb) => (ca?.c_col_index || 0) - (cb?.c_col_index || 0)));
	const sortedComponents = Object.keys(cacheComponents)?.sort((a, b) => +a - +b)?.map?.(k => cacheComponents[k]);

	Object.values(cacheComponentsHead)?.forEach(rowCols => rowCols?.sort((ca, cb) => (ca?.c_col_index || 0) - (cb?.c_col_index || 0)));
	const sortedComponentsHead = Object.keys(cacheComponentsHead)?.sort((a, b) => +a - +b)?.map?.(k => cacheComponentsHead[k]);

  return ({ Components: sortedComponents, ComponentsHeade: sortedComponentsHead });
};


export const lowcodeDebounceAsync = (fn, delay) => {
  let timer = null;

	return  (...args) => new Promise((resolve, reject) => {
		clearTimeout(timer);
    timer = setTimeout(() => {
			const runRes = fn.apply(this, args);
			console.log("lowcodeDebounce runRes ", runRes);
      resolve(runRes);
    }, delay)
	});
}

export const lowcodeDebounce = (fn, delay) => {
  let timer = null;

  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
}

export const lowcodeDefStatus = (dataObj: { [key: string]: any }) => ({
	c_status: (dataObj?.c_status || "")?.trim?.() || ECBillStatus.NotApproved,
	c_approve_status: (dataObj?.c_approve_status || "")?.trim?.() || ECBillApproveStatus.NotYetApproved,
});


export const setLowcodeRowKey =(array: {}[])=>{
	return array.map((m,i)=>({...m, [lowcodeRowKey]: i}))
}

export const handlePanelEditFields = (editFields: { [key: string]: LowcodePagePanelComponentItem }, components?: LowcodePagePanelComponentItem[]) => {
	if (!components || !Array.isArray(components) || !components?.length) {
		return;
	}
	components?.forEach(field => {
		if (field?.c_hide !== "1" && field?.c_code) {
			editFields[field.c_code] = field;
		}
	});
};
